import { defineStore } from 'pinia'
import $ajax from '@/api/to8020.js'
import { showErrorMsg } from '@/utils/alterMessage.js'

export const useGridStore = defineStore('grid', {
  state: () => ({
    grids: [],
    gridValue: []
  }),
  getters: {
    gridTree(state) {
      return state.grids
    }
  },
  actions: {
    clearData() {
      this.gridValue = []
    },
    async loadData() {
      if (this.grids === null || this.grids.length === 0) {
        const path = '/grid/select/all/tree'
        const res = await $ajax.get(path)
        if (res !== null && res.code != null && res.code === 200) {
          this.grids = res.result.children
        } else {
          showErrorMsg('获取网格数据失败')
        }
      }
    }
  }
})
